package com.kanshu.ksgb.fastread.doudou.app.test;

import com.kanshu.ksgb.fastread.doudou.common.business.manager.CacheManager;
import com.kanshu.ksgb.fastread.doudou.module.book.bean.SimpleChapterBean;
import com.kanshu.ksgb.fastread.doudou.module.reader.page.TxtChapter;
import com.kanshu.ksgb.fastread.doudou.module.reader.utils.SettingManager;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.Reader;

public class ChapterLoadUtils {

    public static synchronized void loadChapterContent(String bookId, int chapterPos, LoadChapterContentCallBack callBack) throws Exception {
        // 判断章节本地是否存在
        if (!hasChapterData(bookId, chapterPos)) {
            return;
        }
        TxtChapter chapter = getTxtChapter(bookId, chapterPos);
        if (chapter == null) {
            return;
        }
        new Thread(new Runnable() {
            @Override
            public void run() {
                BufferedReader reader = null;
                StringBuilder sb = null;
                try {
                    // 获取章节的文本流
                    reader = getChapterReader(chapter);
                    sb = new StringBuilder();
                    String tmp;
                    while ((tmp = reader.readLine()) != null) {
                        sb.append(tmp).append("\r\n");
                    }
                    callBack.onNext(sb.toString(),chapter.title);
                    reader.close();
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        }).start();

    }

    public static boolean hasChapterData(String bookId, int chapterIndex) {
        return CacheManager.getInstance().getChapterFile(bookId, chapterIndex) != null;
    }

    public static BufferedReader getChapterReader(TxtChapter chapter) throws Exception {
        File file = CacheManager.getInstance().getChapterFile(chapter.bookId, chapter.chapterIndex);
        if (!file.exists()) return null;
        Reader reader = new FileReader(file);
        BufferedReader br = new BufferedReader(reader);
        return br;
    }

    /**
     * 根据章节序号获取章节内容
     *
     * @param chapterPos
     * @return
     */
    public static TxtChapter getTxtChapter(String bookId, int chapterPos) {
        SimpleChapterBean bean = getSimpleChapterBean(bookId, chapterPos);
        if (bean != null) {
            TxtChapter chapter = TxtChapter.obtain();
            chapter.bookId = bookId;
            chapter.chapterIndex = chapterPos;
            chapter.title = bean.title;
            return chapter;
        }
        return null;
    }

    /**
     * 获取简单章节参数信息
     *
     * @param bookId
     * @param chapterPos
     * @return
     */
    public static SimpleChapterBean getSimpleChapterBean(String bookId, int chapterPos) {
        SimpleChapterBean bean = SettingManager.getInstance().getSimpleChapterInfo(bookId, chapterPos);
        return bean;
    }

    interface LoadChapterContentCallBack {
        void onNext(String res,String chapterTitle);
    }
}
